package com.md.infrastructure.persistence.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.md.base.BaseRepositoryImpl;
import com.md.domain.model.po.RoleModulePO;
import com.md.domain.repository.RoleModuleRepository;
import com.md.infrastructure.persistence.mapper.RoleModuleMapper;
import com.md.util.ObjectUtil;
import com.md.value.server.ServerContext;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 刘骄阳
 * 2022-06-12 1:22
 */
@Repository
public class DAORoleModuleRepository extends BaseRepositoryImpl<RoleModulePO, RoleModuleMapper>
  implements RoleModuleRepository {
  public DAORoleModuleRepository(final RoleModuleMapper mapper) {
    super(mapper);
  }

  @Override
  public Integer checkPermission(final Long userId, final Long moduleId) {
    return mapper.checkPermission(userId, moduleId);
  }

  @Override
  public List<RoleModulePO> selectUserModulePermission(final Long userId) {
    return mapper.selectUserModulePermission(userId);
  }

  @Override
  public void delRoleModuleLink(final List<Long> roleIdList, final List<Long> delModuleIdList) {
    if (ObjectUtil.isEmpty(delModuleIdList)) {
      return;
    }
    LambdaQueryWrapper<RoleModulePO> wrapper = new LambdaQueryWrapper<>();
    wrapper.in(RoleModulePO::getRoleId, roleIdList).in(RoleModulePO::getModuleId, delModuleIdList);
    mapper.delete(wrapper);
  }

  @Override
  public void updateTypeByIdList(final List<Long> updateRoleModuleList, final Integer type) {
    if (ObjectUtil.isEmpty(updateRoleModuleList)) {
      return;
    }
    LambdaUpdateWrapper<RoleModulePO> wrapper = new LambdaUpdateWrapper<>();
    wrapper.in(RoleModulePO::getId, updateRoleModuleList).set(RoleModulePO::getType, type)
      .set(RoleModulePO::getUpdateDate, LocalDateTime.now()).set(RoleModulePO::getUpdateBy,
        ServerContext.getUserId());
    mapper.update(null, wrapper);
  }

  @Override
  public List<RoleModulePO> selectRoleModule(final Long roleId) {
    LambdaQueryWrapper<RoleModulePO> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(RoleModulePO::getRoleId, roleId);
    return mapper.selectList(wrapper);
  }
}
